#!/bin/bash

set -e

if [ "$(id -u)" != "0" ]; then
	echo "this script must be run as root" 1>&2
	exit 1
fi

SELINUX=$(getenforce)
if [[ $SELINUX == "Enforcing" ]]; then
	echo "Please disable selinux"
	exit 1
fi

yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

if [[ $# != 1 ]]; then
	echo "Usage: nginx-install.sh nginx.tgz"
	exit 1
fi


echo "解压 nginx..."
tar -xzf ${1} 

echo "修改c文件..."
[ $? -eq 0 ] && cd nginx-*
[ $? -eq 0 ] && sed -i 's/"Server: nginx"/"Server: unknow"/g' src/http/ngx_http_header_filter_module.c
[ $? -eq 0 ] && sed -i 's/<center>nginx/<center>unknow/g' src/http/ngx_http_special_response.c

echo "安装 nginx..."
[ $? -eq 0 ] && ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[ $? -eq 0 ] && make && make install

echo "隐藏Header头文件..."
[ $? -eq 0 ] && sed -i "/default_type*/a\    server_tokens off;" /usr/local/nginx/conf/nginx.conf
[ $? -eq 0 ] && sed -i "/server_tokens*/a\    include /usr/local/nginx/conf.d/*.conf;" /usr/local/nginx/conf/nginx.conf

echo "配置系统nginx命令..."
[ $? -eq 0 ] && cp /usr/local/nginx/sbin/nginx /usr/local/sbin/

echo "Successfully"
